package com.wu.Backstage.controller;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.wu.Backstage.entity.Po.CommodityEntityPo;
import com.wu.Backstage.entity.Po.OrderDetailsPo;
import com.wu.Backstage.service.CommodityService;
import com.wu.Backstage.service.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping(value = "/code")
public class CodeController {


    @Autowired
    private OrderDetailsService orderDetailsService;

    @GetMapping("/qrcode")
    public void get(HttpServletResponse response,Integer id,String email) throws Exception {


        OrderDetailsPo order =  orderDetailsService.findOrderDetailsId(id);

        HashMap<String,Object> map = new HashMap<String,Object>(0);
        map.put("订单编号",order.getOrdrNum());
        map.put("商品编号",order.getDityId());
        map.put("商品名称",order.getSpname());
        map.put("下单时间",order.getOrderTime());
        map.put("下单数量",order.getOrdrNum());
        map.put("订单状态","正常");
        map.put("下单金额",order.getOrderMonry());
        map.put("下单人",order.getOrderuser());
        map.put("收货地址",order.getUserAddress());


        int width = 200;
        int height = 200;
        String format = "png";

        ServletOutputStream out = response.getOutputStream();
        Map<EncodeHintType,Object> config = new HashMap<>();
        config.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        config.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        config.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(String.valueOf(map), BarcodeFormat.QR_CODE,width,height,config);
        MatrixToImageWriter.writeToStream(bitMatrix,format,out);
        System.out.println("二维码生成完毕，已经输出到页面中。");


    }

}
